package com.yuanzong.wuliu.common.utils.password;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;

public class PasswordEncoderUtils {

    static final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * TODO 文本加密
     *
     * @param text
     * @return
     */
    public static String passwordEncode(String text) {
        return passwordEncoder.encode(text);
    }

    /**
     * TODO 密文验证
     *
     * @param plaintext
     * @param ciphertext
     * @return
     */
    public static boolean passwordVerify(String plaintext, String ciphertext) {
        if (StringUtils.isEmpty(plaintext) || StringUtils.isEmpty(ciphertext)) {
            return false;
        }
        return passwordEncoder.matches(plaintext, ciphertext);
    }

    public static void main(String[] args) {
        String text = "zhaonabei";
        String passwd = passwordEncode(text);
        System.out.println(passwd);
        System.out.println(passwordVerify(text, passwd));
    }

}
